﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

/*
 * Image retrieved from:
 * Saw 8. Digital image. Schmoes Know. Mendoza Agency, 17 Mar. 2014. Web. 29 Sept. 2014.
 * <http://schmoesknow.com/saw-8-coming-to-a-theatre-near-you-in-spring-of-2015/19655/>.
 * */

namespace CSE360_SudokuProject
{
    public partial class LogInMenu : Form
    {
        public LogInMenu()
        {
            InitializeComponent();
        }

        private void LogInMenu_Load(object sender, EventArgs e)
        {
            //makes the label transparent
            var pos = this.PointToScreen(label2.Location);
            pos = pictureBox2.PointToClient(pos);
            label2.Parent = pictureBox2;
            label2.Location = pos;
            label2.BackColor = Color.Transparent;

            //makes the label transparent
            var post = this.PointToScreen(label1.Location);
            post = pictureBox2.PointToClient(post);
            label1.Parent = pictureBox2;
            label1.Location = post;
            label1.BackColor = Color.Transparent;

            FileManager.GetHighScore(); //loads highscores on file or default values
        }

        private void NewUserButton_Click(object sender, EventArgs e)
        {
            if (!IsTextboxEmpty())//function to check if textboxes our inputted
            {   //check if username is new and saves info
                if(FileManager.CreateNewUser(UserNameTextBox.Text, PasswordTextBox.Text))
                    IniMainMenu(); //function to go to main menu
                else
                    //if username is not new outputs this message
                    MessageBox.Show("That username already exits");    
            }
        }

        private void LogInButton_Click(object sender, EventArgs e)
        {
            if (!IsTextboxEmpty())//function to check if textboxes our inputted
            {
                //check if username and password are correct
                if (FileManager.CheckUser(UserNameTextBox.Text, PasswordTextBox.Text))
                    IniMainMenu(); //function to go to main menu
                else
                    //if infomation is wrong display this message
                    MessageBox.Show("Incorrect user information");    
            }
        }

        private bool IsTextboxEmpty()//checks the texboxs to makesure something is inputted
        {
            //checks if either username or password is empty
            if (UserNameTextBox.Text == "" || PasswordTextBox.Text == "")
            {
                if (UserNameTextBox.Text == "" && PasswordTextBox.Text == "") 
                    //case: both username and password are empty
                    MessageBox.Show("Please enter a username and password");
                else if (UserNameTextBox.Text == "")
                    //case: username is empty
                    MessageBox.Show("Please enter a username");
                else
                    //case: password is empty
                    MessageBox.Show("Please enter a password");

                return true;
            }
            return false;
        }

        private void IniMainMenu()//resets text box, hides login and creats mainmenu
        {
            //resets textboxes
            UserNameTextBox.Text = "";
            PasswordTextBox.Text = "";
            //create new mainmenu and show it
            MainMenu theMainMenu = new MainMenu(this);
            theMainMenu.Show();
            //hide loginmenu and disable
            this.Enabled = false;
            this.Visible = false;
        }
    }
}
